/*
 * Zirco Browser for Android
 * 
 * Copyright (C) 2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.xtown.bingluo.xtieba.model.adapters;

import org.xtown.bingluo.xtieba.R;
import org.xtown.bingluo.xtieba.db.XtiebaAuthorDb;
import org.xtown.bingluo.xtieba.db.XtiebaPostsDb;
import org.xtown.bingluo.xtieba.model.XtiebaAuthor;
import org.xtown.bingluo.xtieba.ui.components.MyTextView;
import org.xtown.bingluo.xtieba.utils.DateUtils;
import org.xtown.bingluo.xtieba.utils.IOUtils;
import org.xtown.bingluo.xtieba.utils.UiUtils;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.text.ClipboardManager;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class PostsCursorAdapter extends SimpleCursorAdapter {
	
	private Context mContext;
	
	/**
	 * Constructor.
	 * @param context The context.
	 * @param layout The layout.
	 * @param c The Cursor. 
	 * @param from Input array.
	 * @param to Output array.
	 */
	public PostsCursorAdapter(Context context, int layout, Cursor c, String[] from, int[] to) {
		super(context, layout, c, from, to);
		mContext = context;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View superView = super.getView(position, convertView, parent);

		//图标
		ImageView thumbnailView = (ImageView) superView.findViewById(R.id.PostsRow_Thumbnail);
		
		String author = getCursor().getString(getCursor().getColumnIndex(XtiebaPostsDb.AUTHOR));
		XtiebaAuthor o = XtiebaAuthorDb.getNetInstance(mContext).selectByName(author);
		String local_avatar = "";
		if (o != null && !o.local_avatar.equals("")) {
			local_avatar = o.local_avatar;
		}
		Bitmap bmp = IOUtils.makeAuthorAvatar(mContext, local_avatar);
		if (bmp != null) {
			thumbnailView.setImageBitmap(bmp);
		}
		
		//kzid
		final TextView PostsRow_KZID = (TextView) superView.findViewById(R.id.PostsRow_KZID);
		PostsRow_KZID.setText(getCursor().getString(getCursor().getColumnIndex(XtiebaPostsDb.KZID)));
		PostsRow_KZID.setOnLongClickListener(new OnLongClickListener() {
			
			@Override
			public boolean onLongClick(View arg0) {
				ClipboardManager cbm = (ClipboardManager) mContext.getSystemService(mContext.CLIPBOARD_SERVICE);
				cbm.setText(PostsRow_KZID.getText().toString());
				UiUtils.getInstance(mContext).CreateShowToast(R.string.CrawlActivity_Copyed, Toast.LENGTH_SHORT);
				return true;
			}
		});
		
		//时间
		TextView dateView = (TextView) superView.findViewById(R.id.PostsRow_Date);
		int time = getCursor().getInt(getCursor().getColumnIndex(XtiebaPostsDb.POSTS_CREATED_AT));
		dateView.setText(String.format(mContext.getString(R.string.PostsCursorAdapter_Publish), DateUtils.formatDateForUnixTimestamp(mContext, time)));
		TextView PostsRow_UpdateDate = (TextView) superView.findViewById(R.id.PostsRow_UpdateDate);
		int time2 = getCursor().getInt(getCursor().getColumnIndex(XtiebaPostsDb.UPDATED_AT));
		PostsRow_UpdateDate.setText(String.format(mContext.getString(R.string.PostsCursorAdapter_CrawlUpdate), DateUtils.formatDateForUnixTimestamp(mContext, time2)));
		
		superView.setTag(getCursor().getString(getCursor().getColumnIndex(XtiebaPostsDb.KZID)));
		
		return superView;
	}

}
